package cn.iocoder.yudao.module.erp.controller.admin.gi.ginj;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.erp.controller.admin.gi.ginj.vo.GiNjPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.gi.ginj.vo.GiNjRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.gi.ginj.vo.GiNjSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.gi.GiNjDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.giitem.GiItemNjDO;
import cn.iocoder.yudao.module.erp.service.gi.GiNjService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.io.IOException;
import java.util.List;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - ERP北疆口岸仓库")
@RestController
@RequestMapping("/erp/gi-nj")
@Validated
public class GiNjController {

    @Resource
    private GiNjService giNjService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP北疆口岸仓库")
//    @PreAuthorize("@ss.hasPermission('erp:gi-nj:create')")
    public CommonResult<String> createGiNj(@Valid @RequestBody GiNjSaveReqVO createReqVO) {
        return success(giNjService.createGiNj(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP北疆口岸仓库")
    // @PreAuthorize("@ss.hasPermission('erp:gi-nj:update')")
    public CommonResult<Boolean> updateGiNj(@Valid @RequestBody GiNjSaveReqVO updateReqVO) {
        giNjService.updateGiNj(updateReqVO);
        return success(true);
    }

    @PutMapping("/examine")
    @Operation(summary = "ERP物流南疆仓库单据审核")
    // @PreAuthorize("@ss.hasPermission('erp:gi-nj:examine')")
    public CommonResult<Boolean> examineGiNj(@Valid @RequestBody GiNjDO giNjDO) {
        giNjService.examineGiNj(giNjDO);
        return success(true);
    }
    
    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP北疆口岸仓库")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('erp:gi-nj:delete')")
    public CommonResult<Boolean> deleteGiNj(@RequestParam("id") String id) {
        giNjService.deleteGiNj(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP北疆口岸仓库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('erp:gi-nj:query')")
    public CommonResult<GiNjRespVO> getGiNj(@RequestParam("id") String id) {
        GiNjDO giNj = giNjService.getGiNj(id);
        return success(BeanUtils.toBean(giNj, GiNjRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP北疆口岸仓库分页")
    // @PreAuthorize("@ss.hasPermission('erp:gi-nj:query')")
    public CommonResult<PageResult<GiNjRespVO>> getGiNjPage(@Valid GiNjPageReqVO pageReqVO) {
        PageResult<GiNjDO> pageResult = giNjService.getGiNjPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GiNjRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP北疆口岸仓库 Excel")
    // @PreAuthorize("@ss.hasPermission('erp:gi-nj:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportGiNjExcel(@Valid GiNjPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GiNjDO> list = giNjService.getGiNjPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP北疆口岸仓库.xls", "数据", GiNjRespVO.class,
                        BeanUtils.toBean(list, GiNjRespVO.class));
    }

    // ==================== 子表（ERP物流南疆仓库货号详情） ====================

    @GetMapping("/gi-item-nj/list-by-no")
    @Operation(summary = "获得ERP物流南疆仓库货号详情列表")
    @Parameter(name = "no", description = "入库编号")
    // @PreAuthorize("@ss.hasPermission('erp:gi-nj:detail')")
    public CommonResult<List<GiItemNjDO>> getGiItemNjListByNo(@RequestParam("no") String no) {
        return success(giNjService.getGiItemNjListByNo(no));
    }

}